package com.chainyoung.component_tmperson.ui.adapter;

import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chainyoung.common.imageloader.ImageLoader;
import com.chainyoung.common.imageloader.glide.GlideImageConfig;
import com.chainyoung.common.utils.Utils;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.entity.CoinItem;

import java.util.List;

public class SelectCoinAdapter extends BaseQuickAdapter<CoinItem, BaseViewHolder> {

    private ImageLoader imageLoader;
    private OnSelectListener onSelectListener;
    private boolean isLocal;
    private boolean isCreatedWallet;
    private boolean isImportWallet;
    private boolean isAddWallet;

    public SelectCoinAdapter(@Nullable List<CoinItem> data) {
        super(R.layout.tmperson_list_select_coin_item, data);
        imageLoader = Utils.getAppComponent().imageLoader();
    }

    public SelectCoinAdapter(@Nullable List<CoinItem> data, boolean isLocal) {
        super(R.layout.tmperson_list_select_coin_item, data);
        imageLoader = Utils.getAppComponent().imageLoader();
        this.isLocal = isLocal;
    }

    public SelectCoinAdapter(@Nullable List<CoinItem> data, boolean isLocal, boolean isCreatedWallet) {
        super(R.layout.tmperson_list_select_coin_item, data);
        imageLoader = Utils.getAppComponent().imageLoader();
        this.isLocal = isLocal;
        this.isCreatedWallet = isCreatedWallet;
    }

    public SelectCoinAdapter(@Nullable List<CoinItem> data, boolean isLocal, boolean isCreatedWallet,boolean isImportWallet,boolean isAddWallet) {
        super(R.layout.tmperson_list_select_coin_item, data);
        imageLoader = Utils.getAppComponent().imageLoader();
        this.isLocal = isLocal;
        this.isCreatedWallet = isCreatedWallet;
        this.isImportWallet=isImportWallet;
        this.isAddWallet=isAddWallet;
    }


    @Override
    protected void convert(final BaseViewHolder helper, final CoinItem item) {

        imageLoader.loadImage(mContext,
                GlideImageConfig
                        .builder()
                        .url(item.getIcon())
                        .imagerView((ImageView) (helper.getView(R.id.iv_logo)))
                        .build());

        helper.setText(R.id.tv_simple_name, item.getCoin().toUpperCase());
        helper.setText(R.id.tv_total_name, item.getName());
        if (isCreatedWallet||isImportWallet) {
            ((ImageView) helper.getView(R.id.iv_select)).setImageResource(R.mipmap.ic_arrow_right);
        } else {
            ((ImageView) helper.getView(R.id.iv_select)).setImageResource(item.isIs_select() ? R.mipmap.iv_select : R.mipmap.iv_select_no);
        }
        helper.getView(R.id.rl_select_coin).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (isLocal) {
                    if (isCreatedWallet) {
                        //创建本地钱包
                        if (onSelectListener != null) {
                            onSelectListener.onCreate(helper.getAdapterPosition(), item);
                            return;
                        }
                    }
                    if (isImportWallet){
                        //本地钱包
                        if (onSelectListener != null) {
                            onSelectListener.onImport(helper.getAdapterPosition(), item);
                            return;
                        }
                    }
                    if (isAddWallet){
                        //本地钱包
                        if (onSelectListener != null) {
                            onSelectListener.onAdd(helper.getAdapterPosition(), item);
                            return;
                        }
                    }
                }

                //云端钱包
                item.setIs_select(!item.isIs_select());
                ((ImageView) helper.getView(R.id.iv_select)).setImageResource(item.isIs_select() ? R.mipmap.iv_select : R.mipmap.iv_select_no);
                if (onSelectListener != null) {
                    onSelectListener.onSelect(helper.getAdapterPosition(), item.getCoin());
                }
            }
        });

    }

    public OnSelectListener getOnSelectListener() {
        return onSelectListener;
    }

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
    }

    public interface OnSelectListener {
        void onSelect(int p, String coin);

        void onCreate(int p, CoinItem item);

        void onImport(int p,CoinItem item);

        void onAdd(int p,CoinItem item);
    }
}
